<?php
	function tax_add() {
		global $kiwee;

		$code = addslashes(trim($_POST['postalcode']));
		$desc = addslashes(trim($_POST['description']));
		$rate = floatval($_POST['rate']);
		
		if ($rate < 0)
		  die('ERROR: Please supply a valid rate.');
		
		$qry = 'insert into ' . $kiwee->config['fpx'] . 'taxes (postalcode, description, rate) values ("' .
		  		$code . '","' . $desc . '",' . $rate . ')';
		mysql_query($qry);
	}		

	function tax_delete() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		
		$qry = 'delete from ' . $kiwee->config['fpx'] . 'taxes where id=' . $id;
		mysql_query($qry);
	}		

	function tax_gridload() {
		global $kiwee, $_lang;
		
		$sql = 'SELECT * FROM ' . $kiwee->config['fpx'] . 'taxes order by postalcode';
		$ds = mysql_query($sql) or die ('MYSQL: ' . mysql_error());
		$grd = new DataGrid('',$ds, $number_of_results);
		$grd->noRecordMsg = $_lang['no_records_found'];
		$grd->cssClass='grid';
		$grd->columnHeaderClass='gridHeader';
		$grd->itemClass='gridItem';
		$grd->altItemClass='gridAltItem';
		$grd->fields='postalcode, description, rate';
		$grd->columns= $_lang['kiwee_tax_fld_postalcode'] . ', ' . $_lang['description'] . ', ' . $_lang['kiwee_tax_fld_rate'] .
						',' . $_lang['kiwee_properties'] . ',' . $_lang['delete'];
		$grd->colWidths='100,300,50,50,50';
		$grd->colAligns='left,left,left,center,center';
		$grd->colTypes=',,,template:<button onClick="tax_properties([+id+])">' . $_lang['kiwee_properties'] . '</button>,' .
					   'template:<button onClick="tax_delete([+id+])">' . $_lang['delete'] . '</button>';
		echo $grd->render();
	}

	function tax_properties() {
		global $kiwee, $_lang;
		
		$id = intval($_POST['id']);

		$qry = 'select * from ' . $kiwee->config['fpx'] . 'taxes where id=' . $id;
		$rows = db_getrows($qry);
		if (sizeof($rows) < 1) die("Could not query taxes table");

		$lines = array();
		$lines[] = '<table class="lbform"><tbody>';
		$lines[] = '<tr><td><label for="upostalcode">' . $_lang['kiwee_tax_fld_postalcode'] . ':</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="upostalcode" name="upostalcode" type="text" value="' . $rows[0]['postalcode'] . '" length=20 /></td></tr>';
		$lines[] = '<tr><td><label for="udesc">' . $_lang['description']. ':</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="udesc" name="udesc" type="text" value="' . $rows[0]['description'] . '" length=20 /></td></tr>';
		$lines[] = '<tr><td><label for="urate">' . $_lang['kiwee_tax_fld_rate']. ':</label></td>';
		$lines[] = '<td width=20>&nbsp;</td>';
		$lines[] = '<td><input id="urate" name="urate" type="text" value="' . $rows[0]['rate'] . '" length=20 /></td></tr>';
		$lines[] = '</tbody></table>';
		$lines[] = '<button onclick="tax_update(' . $_POST['id'] . ')">' . $_lang['save'] . '</button>';
		$lines[] = '<button onclick="hideBox()">' . $_lang['cancel'] . '</button>';
		echo implode("\n", $lines);
	}

	function tax_update() {
		global $kiwee;
		
		$code = addslashes(trim($_POST['postalcode']));
		$desc = addslashes(trim($_POST['description']));
		$rate = floatval($_POST['rate']);

		if ($rate < 0)
		  die('ERROR: Please supply a valid rate.');
		
		$qry = 'update ' . $kiwee->config['fpx'] . 'taxes set postalcode="' . $code . '",description="' . $desc . '", rate=' . $rate . ' where id=' . $_POST['id'];
		mysql_query($qry);
	}

	$oktxns[] = 'tax_add';
	$oktxns[] = 'tax_gridload';
	$oktxns[] = 'tax_delete';
	$oktxns[] = 'tax_properties';
	$oktxns[] = 'tax_update';
?>